package jea.machine.gui;

import java.lang.reflect.InvocationTargetException;

import javax.swing.JDesktopPane;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.SwingUtilities;

import jea.machine.transverseprofileviewer.FrameLayoutQuery;
import cern.accsoft.gui.frame.AppFrame;
import cern.accsoft.gui.frame.FrameManager;
import cern.accsoft.gui.frame.MessageManager;

public class MainGui
{
   private static final long serialVersionUID = 1L;
   boolean resizable = true;
   boolean closeable = true;
   boolean maximizable  = true;
   boolean iconifiable = true;
  
   static public JDesktopPane desktop;
   private AppFrame appFrame;
    
	public MainGui() 
	{
		initGUI();
	}
	
	private void initGUI() 
	{
		/*
	    //UIManager.put("activeCaption", new javax.swing.plaf.ColorUIResource(Color.RED));
	    //UIManager.put("activeCaptionText", new javax.swing.plaf.ColorUIResource(Color.YELLOW));		
		// marche pas UIManager.put("InternalFrame.activeTitleBackground", new ColorUIResource(Color.RED));
		// marche pas UIManager.put("InternalFrame.activeTitleForeground", new ColorUIResource(Color.WHITE));
		//UIManager.put("InternalFrame.titleFont", new Font("Dialog", Font.BOLD, 9));
		try 
		{
			//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			//UIManager.setLookAndFeel("javax.swing.plaf.gtk.GTKLookAndFeel");
			UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
			//UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
			//UIManager.setLookAndFeel("javax.swing.plaf.motif.MotifLookAndFeel");
		} catch (Exception e) { System.out.println("");	}
	    */
		// Splash
		Splash splash = new Splash(0);
		
        // invokeAndWait pour afficher le plus tot possible le desktop
	    try {
			SwingUtilities.invokeAndWait(new Runnable() {
				public void run() 
				{
			    // Desktop
			    desktop = new JDesktopPane();      
				appFrame = FrameManager.getInstance().getMainFrame("SPS",desktop);
				appFrame.setSize(1100, 900);
				appFrame.setVisible(true);
	
			/*
			    MenuListener listener = new MenuListener() 
			    {
			      public void menuCanceled(MenuEvent e) {
			        dumpInfo("Canceled", e);
			      }

			      public void menuDeselected(MenuEvent e) {
			        dumpInfo("Deselected", e);
			      }

			      public void menuSelected(MenuEvent e) {
			        dumpInfo("Selected", e);
			      }

			      private void dumpInfo(String s, MenuEvent e) {
			        JMenu menu = (JMenu) e.getSource();
			        System.out.println(s + ": " + menu.getText());
			      }
			    };
				*/
				
				MainGuiMenuListener menuListener = new MainGuiMenuListener(desktop);			
				
				JMenuBar menuBar = new JMenuBar();
				 JMenu menuFile = new JMenu("File");
				 
				 
				  JMenu item1 = new JMenu("Database...");				  
				  //item1.addActionListener(menuListener);
				 
				   JRadioButtonMenuItem rbMenuItemProdDB = new JRadioButtonMenuItem("PROD DataBase");
				   rbMenuItemProdDB.setSelected(true);
				   JRadioButtonMenuItem rbMenuItemDevDB = new JRadioButtonMenuItem("DEV DataBase");
				   rbMenuItemDevDB.setSelected(false);
				 
				   item1.add(rbMenuItemProdDB);
				   item1.add(rbMenuItemDevDB);

				  
				  JMenuItem item2 = new JMenuItem("- not defined -");
				  item2.addActionListener(menuListener);
				  
				  JMenuItem exit = new JMenuItem("Exit");
				  exit.addActionListener(menuListener);
				  

				  menuFile.add(item1);
				  menuFile.add(item2);
				  menuFile.addSeparator();
				  menuFile.add(exit);
				  
				 JMenu menuVacuumProfile = new JMenu("Vacuum Chamber Profiles");
				  JMenuItem menuItemSearchDisplayVacProfileType = new JMenuItem("Search a Vacuum Profile by TYPE...");
				  menuItemSearchDisplayVacProfileType.addActionListener(menuListener);
				  
				  JMenuItem menuItemSearchDisplayVacProfileDesc = new JMenuItem("Search a Vacuum Profile by DESCRIPTION...");
				  menuItemSearchDisplayVacProfileDesc.addActionListener(menuListener);
				  
				  JMenuItem menuItemSearchDisplayVacProfileSPSLoc = new JMenuItem("Search a Vacuum Profile by SPS LOCATION...");
				  menuItemSearchDisplayVacProfileSPSLoc.addActionListener(menuListener);
				  
				  JMenuItem menuItemSearchDisplayVacProfileSPSMetric = new JMenuItem("Search a Vacuum Profile by METRIC LOCATION...");
				  menuItemSearchDisplayVacProfileSPSMetric.addActionListener(menuListener);
				  
				  menuVacuumProfile.add(menuItemSearchDisplayVacProfileType);
				  menuVacuumProfile.add(menuItemSearchDisplayVacProfileDesc);
				  menuVacuumProfile.add(menuItemSearchDisplayVacProfileSPSLoc);
				  menuVacuumProfile.add(menuItemSearchDisplayVacProfileSPSMetric);
				 
				  
				 JMenu menuLayout = new JMenu("Layout");
				  JMenuItem menuItemExtractLayout = new JMenuItem("Extract a Layout");
				  menuItemExtractLayout.addActionListener(menuListener);
				  
				  JMenuItem menuItemLONG = new JMenuItem("LONG");
				  menuItemLONG.addActionListener(menuListener);
				  
				  menuLayout.add(menuItemExtractLayout);
				  menuLayout.add(menuItemLONG);
				  
				 // menu1.addMenuListener(listener); //new MainGuiMenu());
				  menuBar.add(menuFile);
				  menuBar.add(menuVacuumProfile);
				  menuBar.add(menuLayout);
				 
				 appFrame.setJMenuBar(menuBar);
				
				}
			});
		} catch (InterruptedException e) {	e.printStackTrace(); }
		  catch (InvocationTargetException e) {	e.printStackTrace(); }
		     	
		    splash.setInfo("Initialisation GUI...");
		    MessageManager.info("Initialisation GUI...",this);
	      
		    
		    /*
		     FIRST WINDOW
		    FrameProfileViewerByType profview = new FrameProfileViewerByType();
		    desktop.add(profview);
		    */
		    
		   FrameLayoutQuery flq = new FrameLayoutQuery();
		   desktop.add(flq);
		    
		    //FrameOuverture ouv = new FrameOuverture();
	        //desktop.add(ouv);
	         
            splash.setInfo("Ready ");
		    MessageManager.info("Ready.",this);
		  
		    appFrame.setVisible(true);  // bring to front 
		    splash.dispose();
		    
	}
	
	public static void main(String[] args) 
	{
		   // Pas utiliser invokeLater si on utilise invokeAndWait 	
		   // SwingUtilities.invokeLater(new Runnable() {
			//public void run() 
			 //{
				MainGui desk = new MainGui();
			 //}
		    //});
	}
	
}
